var totalCount=1;//总记录数
var pageCount=1;//总页数
var currentPage=1;//当前页
//通告列表用的生成页面的一些方法
$(function() {
	
   if(!window.ActiveXObject){
	   $("#selectPanel").css("margin-right","4px");
	   $("#selectPanel").css("margin-top","4px");
	   $("#selectPanel").css("padding-right","0px");
	   $("#deleteAll").css("width","69px");
	   $("#deleteAll").css("padding-left","0px");
    }
   error();
   GetList();
});
function imgshow(){
	var ei = $("#large");
    ei.hide();
    $(".bigimg").mousemove(function(e) {
        ei.css({ top: e.pageY + 10, left: e.pageX + 20 }).html('<img style="border:1px solid gray;max-height:200px; max-width:200px;  " src="' + this.src + '" />').show();
    }).mouseout(function() {
        ei.hide();
    })
}
//======出现弹出框========
function openBgKuang(obj){
	//加载遮罩
	var heights=document.body.scrollHeight;
	var bW=$("body").width();
	if(window.ActiveXObject){
	  bW=$("body").width()-22;
    }
	$("#fullbg").css({width:bW,height:heights+"px",display:"block"});
	$("#"+obj).css({display:"block"});
}
//======出现弹出框结束========
//初始化页面数据
function GetList(){
	
	AwardDwr.getList(currentPage,function(data){
   		totalCount=data.totalRecords;
   		pageCount=data.totalPages;
   		currentPage=data.pageNo;
   		if(currentPage==1){
   			$("#fstPage").attr("src","img/gl/130.jpg");
   			$("#forePage").attr("src","img/gl/140.jpg");
   			$("#fstPage").css("cursor","default");
   			$("#forePage").css("cursor","default");
   		}else{
   			$("#fstPage").attr("src","img/gl/13.jpg");
   			$("#forePage").attr("src","img/gl/14.jpg");
   			$("#fstPage").css("cursor","pointer");
   			$("#forePage").css("cursor","pointer");
   		}
   		if(currentPage==pageCount){
   		  	$("#nextPage").attr("src","img/gl/150.jpg");
   			$("#nextPage").css("cursor","default");
   		}else{
   			$("#nextPage").attr("src","img/gl/15.jpg");
   			$("#nextPage").css("cursor","pointer");
   		}
	   $("#pageinfo").html("共计"+data.totalRecords+"条奖品，当前第"+data.pageNo+"页，共"+data.totalPages+"页。");
	   var trs="";
	   var xuhao=(data.pageNo-1)*data.pageSize+1;
	   for(var i=0;i<data.awards.length;i++){
		   trs=trs+trtemplate.replace("#Title#",data.awards[i].name)
		                     .replace("#Kind#",data.awards[i].levelstr)
		                     .replace("#Img#",ChenImg(data.awards[i].imgurl))
		                     .replace("#Remark#",data.awards[i].remark).replace("#ID#",data.awards[i].id).replace("#ID#",data.awards[i].id)
		                     .replace("#ID#",data.awards[i].id).replace("#ID#",data.awards[i].id).replace("#RowNum#",xuhao++);
	   }
	   $("#ttbody").html(trs);
	   BindCheckBox();
	   imgshow();
   }); 
}
//初始化奖品图片
function ChenImg(imgurl){
	if(imgurl.length<1){
		return "&nbsp;";
	}else{
		return '<img class="bigimg" style="width: 20px; height: 20px; " src="upload/award/'+imgurl+'"';
	}
}
//绑定多选框
function BindCheckBox(){
	        var $chkall = $('#checkedAll');
	        $chkall.unbind("click");
            var $chkarry = $('input[type="checkbox"]').not($('#checkedAll'));
            $chkall.click(function() {
                var b = $(this).attr('checked');
                $chkarry.each(function() { $(this).attr('checked', b); });
            });
}
//跳转页面
function PageGo()
{
	 var page=$("#gopage").val();
	  var regNum =/^\d*$/;
	  if(page.length>0){
		  if(regNum.test(page)){
			  currentPage=parseInt(page)>pageCount?pageCount:parseInt(page);
			  if(currentPage>0){
				  GetList();
			  }else{
				  alert("请输入大于零的数字！");
				  $("#gopage").attr("value","");
				  $("#gopage").focus();
			  }
		  }else{
			  alert("只能输入数字");
			  $("#gopage").attr("value","");
			  $("#gopage").focus();
			  }
		}else{
          alert("请输入您要跳转的页数");
		  $("#gopage").focus();
			}
	  regNum=null;
	  page=null;
}
//翻页
function ChangePage(cmd){
	if("fst"==cmd){
		//首页
		if(currentPage!=1){
			currentPage=1;
			GetList();
		}
	}else if("fore"==cmd){
		//上一页
		if(currentPage!=1){
			currentPage--;
			GetList();
		}
	}else if("next"==cmd){
		//下一页
		if(currentPage!=pageCount){
			currentPage++;
			GetList();
		}
	}
	$('#checkedAll').attr('checked', false);
}
//删除
function Delete(id){
	if(window.confirm("确定要删除该奖品吗?")){
		AwardDwr.Del(id,function(data){
			if(data==1){
				GetList();
				alert("删除成功！");
			}else{
				alert("删除失败！");
			}
		});
	}
}
//删除所选
function DelMore(){
	if(window.confirm("确定要删除所选奖品吗?")){
	    var ids="";
	    var $chkarry = $('input[type="checkbox"]').not($('#checkedAll'));
        $chkarry.each(function() { 
        	if($(this).attr('checked')){
        		ids+=$(this).val()+",";
        	}
        });
        if(ids.length<1){
        	alert("请选择要删除的奖品！");
        }else{
        	AwardDwr.DelMore(ids,function(data){
			if(data>0){
				GetList();
				alert("删除成功！");
			}else{
				alert("删除失败！");
			}
		});
        }
        $chkarry =null;
        ids=null;
     }
}
//修改
function Update(id){
	AwardDwr.GetAwardById(id,function(data){
		if(data!=null){
			$("#kind").val(data.level);
			$("#title").val(data.name);
			$("#remark").val(data.remark);
			$("#tid").val(data.id);
			$("#pagenum").val(currentPage);
			openBgKuang("editpanel");
		}else{
			alert("该通告已被删除！");
		}
	});
}
//update notice
function UpToGao(){
	var title = $("#title").val();
	var remark = $("#remark").val();
	var imgfile = $("#imgfile").val();
	var hasimg = $("#hasimg").attr("checked");
	if(title.length<1){
		alert("奖品名称不能为空！");
		return false;
	}else if(title.length>50){
		alert("奖品名称字数应小于50！");
		return false;
	}
	if(imgfile.length>4){//说明上传了音频文件
		
	    if(imgfile.lastIndexOf(".")==-1)
	    {
	         alert("上传图片格式不正确！");
       		 return false;
	    }
	    var ext=imgfile.substring(imgfile.lastIndexOf(".")+1).toLowerCase();
		if(ext=="gif" || ext=="jpg" || ext=="png") {
			
         }else{
        	 alert("只允许上次gif,jpg,png格式的图片！");
	       		return false;
         }
	 }else{
		//alert("请选择要上传的图片！");	
		//return false;
	}
	$("#updateNotice").submit();
}
//cancel to save notice
function CancelUp(){
	$("#fullbg").css({display:"none"});
	$("#editpanel").css({display:"none"});
	$("#kind").css("display","");
	$("#type").css("display","");
}


//tr模板
var trtemplate='<tr>'+
				'					<td style="width: 40px;">'+
				'						#RowNum#'+
				'					</td>'+
				'					<td style="width: 100px;">'+
				'						#Title#'+
				'					</td>'+
				'					<td style="width: 100px;">'+
				'						#Kind#'+
				'					</td>'+
				'					<td style="width: 100px;">'+
				'						#Img#'+
				'					</td>'+
				'					<td style="">'+
				'						#Remark#&nbsp'+
				'					</td>'+
				'					<td style="width: 100px;">'+
				'						<table class="intable">'+
				'							<tr>'+
				'								<td style="border: 0px;padding-top:2px;padding-right:2px;">'+
				'									<input type="checkbox" value="#ID#">'+
				'								</td>'+
				'								<td style="border: 0px;">'+
				'									<img alt="删除" src="img/gl/10.jpg" class="button" onclick="Delete(#ID#)" />'+
				'								</td>'+
				'								<td style="border: 0px;">'+
				'									<img alt="修改" src="img/gl/12.jpg" class="button" onclick="Update(#ID#)"/>'+
				'								</td>'+
				'							</tr>'+
				'						</table>'+
				'					</td>'+
				'				</tr>';
